package org.datascooter.db.h2;

import java.text.MessageFormat;
import java.util.List;

import org.datascooter.exception.EntityNotMappedException;
import org.datascooter.impl.DataSnip;
import org.datascooter.impl.SnipBuilder;
import org.datascooter.inface.ISnipBuilder;
import org.datascooter.utils.LangUtils;
import org.datascooter.utils.SnipUtils;

public class H2SnipBuilder extends SnipBuilder {

	private static String CREATE_INDEX = "CREATE {0} INDEX IDX_{1} ON {2}({3})";

	public H2SnipBuilder() {
		super("h2");
	}

	@Override
	public ISnipBuilder copy() {
		return new H2SnipBuilder();
	}

	@Override
	public String managePage(DataSnip dataSnip, String sqlClause) {
		if (dataSnip.fetchSize > 0) {
			managePageParams(dataSnip, params);
			return sqlClause + " LIMIT ? OFFSET  ?";
		}
		return sqlClause;
	}

	@Override
	protected String buildDelete(DataSnip dataSnip) throws EntityNotMappedException {
		buildTablesClause(dataSnip);
		buildWhereClause(dataSnip);
		if (dataSnip.joinList.size() > 0) {
			return MessageFormat.format("DELETE FROM  {1} {2}", getTableAlias(dataSnip.bundle.getTableName()) + SnipUtils.DOT_ASTERICS, tables.toString(), where.toString());
		} else {
			return MessageFormat.format(SnipUtils.DELETE_FROM, getCaseSensitiveTableName(dataSnip.bundle.getTableName()), SnipUtils.NN, where.toString());
		}
	}

	@Override
	public void managePageParams(DataSnip dataSnip, List<Object> paramList) {
		paramList.add((Integer) dataSnip.fetchSize);
		paramList.add((Integer) dataSnip.fromRow);
	}

	@SuppressWarnings("nls")
	@Override
	protected void buildColumnIndex(StringBuilder indexBuilder, String name, String type, String column, String tableName) {
		if (indexBuilder.length() > 0) {
			indexBuilder.append(SnipUtils.SEMICOLON + SnipUtils.NL);
		}
		indexBuilder.append(MessageFormat.format(
			CREATE_INDEX,
			LangUtils.defaultIfEmpty(type),
			LangUtils.defaultIfEmpty(name, SnipUtils.NN).toUpperCase() + LangUtils.getRandomIndexString(3),
			tableName,
			column));
	}
	/**
	 * LOWER case search
	 * 
	 * @Override protected void buildConditionClause(EntityBundle parentBundle, Condition condition, int parentCount) throws BuildClauseException { int count = 0; for (LogicValue logicValue :
	 *           condition.getValueList()) { if ((where.length() > 0) && ((parentCount == 0) || (count > 0))) { where.append(SnipUtils.SP + condition.getOperation().name() + SnipUtils.SP); } if
	 *           (logicValue.getConstraint().equals(Constraint.LIKE) || logicValue.getConstraint().equals(Constraint.NOT_LIKE)) { where.append(" LOWER( " + getTableNameWhere(parentBundle) +
	 *           parentBundle.getAttribute(logicValue.getAttribute()).column + " ) " + SnipUtils.SP + checkParamValue(parentBundle, logicValue)); } else { where.append(getTableNameWhere(parentBundle)
	 *           + parentBundle.getAttribute(logicValue.getAttribute()).column + SnipUtils.SP + checkParamValue(parentBundle, logicValue)); } count++; } for (Condition child :
	 *           condition.getChildList()) { if ((condition.getValueList().size() > 0)) { where.append(SnipUtils.SP + condition.getOperation().name() + SnipUtils.LB); }
	 *           buildConditionClause(parentBundle, child, condition.getValueList().size() + parentCount); if ((condition.getValueList().size() > 0)) { where.append(SnipUtils.RB); } } }
	 * @Override
	 * @SuppressWarnings("nls") protected String checkParamValue(EntityBundle nextBundle, LogicValue logicValue) throws BuildClauseException { String paramString = ""; SimpleAttribute
	 *                          columnByAttribute = nextBundle.getAttribute (logicValue.getAttribute()); if (columnByAttribute == null) { throw new BuildClauseException(
	 *                          "The columnByAttribute not found for condition : " + logicValue.getAttribute() + "  " + nextBundle.entity + "   " + nextBundle.getColumnList()); } if
	 *                          (logicValue.getValue() == null) { if (logicValue. getConstraint().equals(Constraint.EQUAL)) { paramString = SnipUtils.IS_NULL; } else if (logicValue
	 *                          .getConstraint().equals(Constraint.NOT_EQUAL)) { paramString = SnipUtils.IS_NOT_NULL; } } else { if (logicValue.getConstraint().equals(Constraint. LIKE) ||
	 *                          logicValue.getConstraint().equals(Constraint .NOT_LIKE)) { paramString = Constraint.toString(logicValue.getConstraint()) + " LOWER(" + SnipUtils.LBQURB + ") "; } else {
	 *                          paramString = Constraint.toString(logicValue.getConstraint()) + SnipUtils.QU; } addParam(columnByAttribute.type, logicValue.getValue()); } return paramString; }
	 */
}
